import { Component, ViewChild, Injector, Output, EventEmitter } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { ToolingServiceProxy, ToolingListDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';

import { finalize } from 'rxjs/operators';

@Component({
  selector: 'editOutOrBackToolingModal',
  templateUrl: './edit-out-or-back-tooling-modal.component.html'
})

export class EditOutOrBackToolingModalComponent extends AppComponentBase {

  @ViewChild('editModal') modal: ModalDirective;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  operationNames = ['', '借出/配备', '返修', '送检', '技改'];

  stockInfo = '';
  remark = '';

  operation = 0;
  type = 0; // 出库 1 入库 2
  tooling = new ToolingListDto();

  constructor(
    injector: Injector,
    private _toolingService: ToolingServiceProxy
  ) {
    super(injector);
  }

  show(tooling: ToolingListDto, operation: number, type: number): void {
    this.active = true;

    this.operation = operation;
    this.type = type;

    this.tooling = tooling;

    // 初始化 信息
    this.stockInfo = '';
    this.remark = '';

    this.modal.show();

  }

  save(): void {
    this.saving = true;

    if (this.type === 1) {
      this._toolingService.outStock(this.tooling.id, this.operation, this.stockInfo, this.remark)
        .subscribe(() => {
          this.notify.success('出库成功');
          this.close();
          this.modalSave.emit(null);
        });
    } else {
      this._toolingService.backStock(this.tooling.id, this.stockInfo, this.remark)
        .subscribe(() => {
          this.notify.success('入库成功');
          this.close();
          this.modalSave.emit(null);
        });
    }
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }
}
